package dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import bean.Distance;
import bean.Road;
import dao.RoadDao;
import exception.DBException;

public class RoadDaoImpl extends HibernateDaoSupport implements RoadDao {
	private StaticData staticData;

	@Override
	public List<Road> selectAllByArea(int area) throws DBException {
		if (staticData == null) {
			@SuppressWarnings("unchecked")
			List<Road> ls = this.getHibernateTemplate().find("from Road");
			List<Road> rtr = new ArrayList<Road>();
			for (Road road : ls) {
				if (road.getPoints().get(0).getArea().getId() != area) {
					rtr.add(road);
				}
			}
			ls.removeAll(rtr);
			return ls;
		}
		return staticData.getAllRoads(area);
	}

	@Override
	public boolean delete(Road road) throws DBException {
		this.getHibernateTemplate().delete(road);
		return true;
	}

	@Override
	public boolean update(List<Road> roads) throws DBException {
		for (Road r : roads) {
			if (!update(r)) {
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean update(Road road) throws DBException {
		if (road.getId() == 0)
			this.getHibernateTemplate().save(road);
		else
			this.getHibernateTemplate().update(road);
		return true;
	}

	@Override
	public List<Road> selectByName(String name) throws DBException {
		@SuppressWarnings("unchecked")
		List<Road> ls = this.getHibernateTemplate().find(
				"from Road where name=?", name);
		return ls;
	}

	@Override
	public List<Road> select(Road road) throws DBException {
		@SuppressWarnings("unchecked")
		List<Road> ls = this.getHibernateTemplate().findByExample(road);
		return ls;
	}

	@Override
	public Road select(int id) throws DBException {
		@SuppressWarnings("unchecked")
		List<Road> ls = this.getHibernateTemplate().find(
				"from Road where id=?", id);
		if (ls != null && ls.size() > 0)
			return ls.get(0);
		return null;
	}

	@Override
	public List<Distance> restoreDistance(List<Distance> distances, int area)
			throws DBException {
		List<Distance> temp = selectDistances(area);
		if (temp != null && temp.size() > 0)
			this.getHibernateTemplate().deleteAll(temp);
		this.getHibernateTemplate().saveOrUpdateAll(distances);
		return distances;
	}

	@Override
	public List<Distance> selectDistances(int area) throws DBException {
		if (staticData == null) {
			@SuppressWarnings("unchecked")
			List<Distance> dis = this.getHibernateTemplate().find(
					"from Distance");
			List<Distance> temp = new ArrayList<Distance>();
			for (Distance d : dis) {
				if (d.getPoint1().getArea().getId() != area)
					temp.add(d);
			}
			dis.removeAll(temp);
			return dis;
		} else {
			return staticData.getAllDistance(area);
		}
	}

	public StaticData getStaticData() {
		return staticData;
	}

	public void setStaticData(StaticData staticData) {
		this.staticData = staticData;
	}

}
